package greedyAlgorithm;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/*
 *@description 划分字母区间
 *@author hkfan
 *@version 1.0
 *@create 2025/5/16 15:57
 */
public class LeetCode_763 {

    // ababcbaca defegde hijhklij
    @Test
    public void test(){
        List<Integer> res = partitionLabels("ababcbacadefegdehijhklij");
        System.out.println("ok "+res.size());
    }

    public List<Integer> partitionLabels(String s) {
        //1. 定义参数
        int limit = 0,before = 0;
        List<Integer> res = new ArrayList<>();
        //2. 循环比较
        for (int i = 0; i <= s.length() - 1; i++) {
            //2.1 判断[i]的最后出现位置
            int location = s.lastIndexOf(s.charAt(i));
            limit = Math.max(limit,location);
            //2.2 是否满足添加，记录结果
            if (limit == i){
                res.add(i-before+1);
                before=i+1;
                limit++;
            }
        }
        return res;
    }

}
